<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input 
        v-model="listQuery.filters.name" 
        placeholder="项目名" 
        style="width: 200px;" 
        class="filter-item"/>
      <el-input 
        v-model="listQuery.filters.userName" 
        placeholder="理财师户名" 
        style="width: 200px;" 
        class="filter-item"/>
      <el-input 
        v-model="listQuery.filters.consumerName" 
        placeholder="用户名" 
        style="width: 200px;" 
        class="filter-item"/>
      <el-select 
        v-model="listQuery.filters.productType" 
        placeholder="项目来源" 
        clearable 
        style="width: 120px" 
        class="filter-item">
        <el-option 
          v-for="item in productTypeList" 
          :value="item.id" 
          :label="item.title"/>
      </el-select>
      <el-select 
        v-model="listQuery.filters.status" 
        placeholder="审核状态" 
        clearable 
        style="width: 120px" 
        class="filter-item">
        <el-option 
          v-for="item in statusList" 
          :value="item.id" 
          :label="item.title"/>
      </el-select>
      <el-button 
        class="filter-item" 
        type="primary" 
        icon="el-icon-search" 
        @click="handleFilter">{{ $t('all.search') }}</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column 
        label="订单id" 
        prop="id" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        label="项目名" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        label="用户名" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.consumerName }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        label="理财师姓名" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        label="项目来源" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.productType }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        label="项目类型" 
        align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.productCategory }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        label="审核状态" 
        align="center">
        <template slot-scope="scope">
          <span>{{ statusFilter(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        :label="$t('all.actions')" 
        align="center" 
        class-name="small-padding fixed-width" 
        width="230">
        <template slot-scope="scope">
          <el-button 
            type="primary" 
            size="medium" 
            @click="handleUpdate(scope.row.id)">详情</el-button>
          <el-button 
            type="warning" 
            size="medium" 
            @click="handleStatus(scope.row)" >{{ scope.row.status==1?'审核通过':'审核不通过' }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <order-detail ref="smgqform"/>
    <pagination 
      v-show="total>0" 
      :total="total" 
      :page.sync="listQuery.page" 
      :limit.sync="listQuery.pageSize" 
      @pagination="getList" />

  </div>
</template>

<script>
import { fetchList, fetch,handleStatus } from '@/api/order'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import OrderDetail from './form'

export default {
  name: 'AdIndex',
  components: { Pagination, OrderDetail },
  data() {
    return {

      imagecropperShow: false,
      imagecropperKey: 0,

      dialogFormVisible: false,
      dialogStatus: '',
      tableKey: 0,
      productTypeList: [
        {
          id: '天赋集团',
          title: '天赋集团'
        },
        {
          id: '其它',
          title: '其它'
        }
      ],
      statusList: [
        {
          id: '1',
          title: '待审核'
        },
        {
          id: '2',
          title: '已审核'
        }
      ],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        order: 'desc',
        sort: 'id',
        filters: {
          name: '', // 项目名
          status: '',
          userName: '',
          consumerName: '',
          productType: ''
        }
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {

    statusFilter(status) {
      if (status == 1) {
        return '待审核'
      } else if (status == 2) {
        return '已审核'
      } else {
        return ''
      }
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },
    handleFilter() {
      this.getList()
    },
    handleUpdate(row) {
      this.$refs.smgqform.formShow(row, '', 3)
    },
    handleStatus(row) {
      this.$confirm('确认执行操作吗?', '提示', {
        type: 'warning'
      }).then(() => {
        handleStatus(row).then((res) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.handleFilter()
        })
      }).catch(() => {

      })
    }
  }
}
</script>

<style scoped>
  .avatar{
    width: 200px;
    height: 200px;
    border-radius: 50%;
  }
</style>
